﻿namespace BM.Tools.WebControls.MForm.BlogExample
{
    using System;    
    using System.Web;
    using System.Web.UI.WebControls;

    /// <summary>
    /// This page is used to display all the blog entries using the <see cref="Repeater"/> control.
    /// </summary>
    public partial class Entries : System.Web.UI.Page
    {
        /// <summary>
        /// Reads the data source
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                blogEntriesRepeater.DataSource = BlogEntries.Get();
                blogEntriesRepeater.DataBind();                
            }
        }

        /// <summary>
        /// Sets the blog entry data binding and sets the visibility of buttons.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected void BindBlogEntry(object sender, RepeaterItemEventArgs e)
        {
            var blogEntry = e.Item.DataItem as BlogEntry;
            var blogControl = e.Item.FindControl("blogEntryControl") as BlogControl;            
            blogControl.BlogEntry = blogEntry;
            blogControl.DataBind();
            var editButton = e.Item.FindControl("EditButton") as Button;
            editButton.Visible = blogControl.AuthorItem.Value == (string)HttpContext.Current.Session["user"];
        }
    }
}
